import { lazy } from "react"
import { createBrowserRouter, Outlet } from "react-router-dom"
// 导入路由
const Login = lazy(() => import("@/page/Login"))
const Layout = lazy(() => import("@/components/Layout"))
const Error = lazy(() => import("@/page/Error"))
// 三级路由
const ArticleList = lazy(() => import("@/page/Article/ArticleList"))
const PosterList = lazy(() => import("@/page/Poster/PosterList"))
const UserList = lazy(() => import("@/page/User/UserList"))
// 搭建路由表
export const routerList = [
  {
    path: "/login",
    element: <Login></Login>,
  },
  {
    path: "/",
    element: <Layout />,
    meta: {
      title: "首页",
      role: [1, 2],
      hidden: true,
    },
    children: [
      // 文章
      {
        path: "article",
        element: <Outlet />,
        meta: {
          title: "文章管理",
          authId: [1, 2],
        },
        children: [
          {
            path: "",
            element: <ArticleList />,
            meta: {
              title: "文章列表",
              authId: [1, 2],
            },
          },
        ],
      },
      // 海报
      {
        path: "poster",
        element: <Outlet />,
        meta: {
          title: "海报管理",
          authId: [1, 2],
        },
        children: [
          {
            path: "",
            element: <PosterList />,
            meta: {
              title: "海报列表",
              authId: [1, 2],
            },
          },
        ],
      },
      // 用户
      {
        path: "user",
        element: <Outlet />,
        meta: {
          title: "用户管理",
          authId: [1],
        },
        children: [
          {
            path: "",
            element: <UserList />,
            meta: {
              title: "用户列表",
              authId: [1],
            },
          },
        ],
      },
    ],
  },
  {
    path: "*",
    element: <Error />,
  },
]
const router = createBrowserRouter(routerList)
export default router
// 路由权限筛选的函数
export const filterRouter = (routes: any[], role: number): any[] => {
  return routes
    .map(route => {
      if (!route.meta?.authId.includes(role)) {
        return null
      }
      if (route.children) {
        const childrenRouter = filterRouter(route.children, role)
        if (childrenRouter.length > 0) {
          return {
            ...route,
            childrenRouter,
          }
        }
        return null
      }
      return route
    })
    .filter(Boolean)
}
// 符合Menu菜单的信息函数
export const MenuRouter = (
  routes: any[],
  parentPath: string = "",
  t: any
): any[] => {
  return routes
    .filter(route => !route.meta?.hidden)
    .map(route => {
      // 拼接完整路径
      const fullPath = `${parentPath}/${route?.path}`
      let label
      if (t) {
        const transtion = `menu.${route.path}`
        label = t(transtion)
      }
      return {
        key: fullPath,
        label: label,
        children: route.children
          ? MenuRouter(route.children, fullPath, t)
          : undefined,
      }
    })
    .filter(Boolean)
}
